package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.vo.AdvanceOrderDTO;
import com.itheima.health.vo.LoginTelephone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.text.ParseException;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

/**
 * @Author 山外小王
 * @Description
 * @Date 2024/04/18/16:17
 */
@RestController
@RequestMapping("/mobile")
public class MobileController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private SetMealService setMealService;
    @Autowired
    private OrderSettingService orderSettingService;
    @GetMapping("/setmeal/getSetmeal")
    public Result getSetmeal(){
        List<Setmeal> data = setMealService.getSetmeal();
        return new Result(true, MessageConst.GET_SETMEAL_LIST_SUCCESS,data);
    }

    @PostMapping("/validateCode/send")
    public Result validateCode(String type, String telephone){
        int code = ThreadLocalRandom.current().nextInt(1000000);
        redisTemplate.opsForHash().put(telephone,type,Integer.toString(code));
        redisTemplate.expire(telephone,600, TimeUnit.SECONDS);
        return new Result(true,MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

    /**
     * 手机号注册登录
     * @param loginTelephone
     * @return
     */
    @PostMapping("/login/smsLogin")
    public Result smsLogin(@RequestBody LoginTelephone loginTelephone){
        String mobile_signin = (String) redisTemplate.opsForHash().get(loginTelephone.getTelephone(), "MOBILE_SIGNIN");
        if (loginTelephone.getValidateCode() != null &&loginTelephone.getValidateCode().equals(mobile_signin)) {
            return new Result(true,MessageConst.LOGIN_SUCCESS);
        } else {
            return new Result(false,MessageConst.LOGIN_FAIL);
        }
    }

    /**
     * c端
     * 查询套餐详细信息
     * @param id
     * @return
     */
    @GetMapping("/setmeal/findById")
    public Result findById(Integer id){
        Setmeal setmeal = setMealService.findDetailById(id);
        return new Result(true,MessageConst.QUERY_SETMEAL_SUCCESS,setmeal);
    }

    /**
     * 预约订单提交
     * @param orderDTO
     * @return
     */
    @PostMapping("/order/submit")
    public Result submit(@RequestBody AdvanceOrderDTO orderDTO) throws ParseException {
        String orderCode = (String) redisTemplate.opsForHash().get(orderDTO.getTelephone(), "ORDER");
        if (orderDTO.getValidateCode() != null &&orderDTO.getValidateCode().equals(orderCode)) {
            return orderSettingService.submit(orderDTO);
        } else {
            return new Result(false,"验证码错误");
        }
    }
}
